package cn.myfirefly.moming.common.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.myfirefly.moming.common.dao.DepartmentDao;
import cn.myfirefly.moming.common.entity.MMDepartment;
import cn.myfirefly.moming.common.model.DepartmentDto;
import cn.myfirefly.moming.common.model.StaffDto;
import cn.myfirefly.moming.common.model.StaffFilterDto;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.BeanUtil;
import cn.myfirefly.utils.StrUtil;

@Service
@Transactional
public class DepartmentService {

	@Autowired
	private DepartmentDao departmentDao;
	
	public List<DepartmentDto> getAll() {
		List<DepartmentDto> resultList = null;
		List<MMDepartment> list = departmentDao.getAll();
		if (list != null) {
			resultList = new ArrayList<DepartmentDto>();
			DepartmentDto dto = null;
			for (MMDepartment dept : list) {
				dto = new DepartmentDto();
				BeanUtil.copyBeans(dept, dto);
				// 设置部门全路径，例如：业务部/销售部/营业部
				dto.setDepartmentNamePath(getDeptPath("", dept, list));
				resultList.add(dto);
			}
		}
		return resultList;
	}
	
	public String save(DepartmentDto dto) {
		MMDepartment dept = departmentDao.getByNo(dto.getDepartmentNo());
		// 更新
		if (StrUtil.notBlank(dto.getDepartmentSn())) {
			if (dept == null) {
				return "部门不存在，请刷新页面后重试。";
			}
			if (dept != null && !dept.getDepartmentSn().equals(dto.getDepartmentSn())) {
				return "部门已变更，请刷新页面后重试。";
			}
		} else {
		// 新增
			if (dept != null) {
				return "部门编号已存在。";
			} 
			dept = new MMDepartment();
		}
		BeanUtil.copyBeans(dto, dept);
		dept.setCreateBy(dto.getModifyBy());
		dept.setCreateDate(dto.getModifyDate());
		Integer departmentSn = departmentDao.save(dept);
		dto.setDepartmentSn(departmentSn);
		return Const.ResultCode.SUCCESS;
	}
	
	public String del(Integer departmentSn) {
		MMDepartment dept = departmentDao.get(departmentSn);
		if (dept == null) {
			return "部门不存在，请刷新页面后重试。";
		}
		List<MMDepartment> list = departmentDao.getChildren(departmentSn);
		if (list != null && list.size() > 0) {
			return "部门下有子部门，不能进行删除。";
		}
		
		departmentDao.del(dept);
		return Const.ResultCode.SUCCESS;
	}
	
	public DepartmentDto getDepartment(Integer departmentSn) {
		MMDepartment dept = departmentDao.get(departmentSn);
		return BeanUtil.copyBeans(DepartmentDto.class, dept);
	}
	
	public String update(DepartmentDto dto) {
		MMDepartment dept = departmentDao.get(dto.getDepartmentSn());
		MMDepartment dept2 = departmentDao.getByNo(dto.getDepartmentNo());
		if (dept == null) {
			return "部门不存在，请刷新页面后重试。";
		}
		if (dept2 != null && !dept2.getDepartmentSn().equals(dept.getDepartmentSn()) &&
				StrUtil.equals(dept2.getDepartmentNo(), dept.getDepartmentNo())) {
			return "部门编号重复！";
		}
		BeanUtil.copyBeans(dto, dept);
		departmentDao.save(dept);
		return Const.ResultCode.SUCCESS;
	}
	
	public PageResultDto<StaffDto> getStaffList(StaffFilterDto dto, Integer departmentSn) {
		
		return departmentDao.getStaffList(dto, departmentSn);
	}
	
	public String updatePId(DepartmentDto dto) {
		departmentDao.updatePId(dto.getDepartmentSn(), dto.getParentDepartSn());
		return Const.ResultCode.SUCCESS;
	}
	
	private String getDeptPath(String deptPath, MMDepartment dept, List<MMDepartment> list) {
		String result = "/" + dept.getDepartmentName() + deptPath;
		if (dept.getParentDepartSn() > 1) {
			for (MMDepartment mmDepartment : list) {
				if (mmDepartment.getDepartmentSn().equals(dept.getParentDepartSn())) {
					result = getDeptPath(result, mmDepartment, list);
					break;
				}
			}
		}
		return result;
	}
}
